package com.sevenstudio.adu.gasing.element;

import java.io.IOException;
import java.io.InputStream;

import org.andengine.entity.sprite.Sprite;

import org.andengine.opengl.texture.ITexture;

import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;

import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.util.adt.io.in.IInputStreamOpener;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


import com.sevenstudio.adu.gasing.engine.Anchor;
import com.sevenstudio.adu.gasing.engine.GameEngine;
import com.sevenstudio.adu.gasing.engine.Utils;
import com.sevenstudio.adu.gasing.game.element.GameElementSprite;

public class ASprite 
{
	Sprite spr;
	
	GameEngine theEngine;

//	BitmapTextureAtlas bbta;
//	TextureRegion ttr;
	
	public ASprite(GameEngine mainActivity, GameElementSprite[] Array_spr, int index)
	{
		this.theEngine = mainActivity;
		Load(Array_spr, index);
	}
	
	public void Load(GameElementSprite[] Array_spr, int index)
	{

		final String path = Array_spr[index].getPath();


//		Bitmap bitmap;

		try {

			Bitmap bitmap = BitmapFactory.decodeStream(theEngine.getAssets().open(Array_spr[index].getPath()));
			ITexture gameSpriteITexture = new BitmapTexture(theEngine.getTextureManager(), new IInputStreamOpener() 
				{
					public InputStream open() throws IOException 
					{
						return theEngine.getAssets().open(path);
					}
				});
			gameSpriteITexture.load();
			ITextureRegion gameSpriteTextureRegion = TextureRegionFactory
					.extractFromTexture(gameSpriteITexture);		
		
			spr = new Sprite(Utils.GetRatio(Array_spr[index].getX()),
					Utils.GetRatio(Array_spr[index].getY()),
					Utils.GetRatio(Array_spr[index].getW()),
					Utils.GetRatio(Array_spr[index].getH()),
					gameSpriteTextureRegion.deepCopy(),
					theEngine.getVertexBufferObjectManager());

			float x, y;
			x = Utils.GetAnchorX(spr.getWidth(), Array_spr[index].getAnchor()) + Utils.GetRatio(Array_spr[index].getX());
			y = Utils.GetAnchorY(spr.getHeight(), Array_spr[index].getAnchor()) + Utils.GetRatio(Array_spr[index].getY());
			
			spr.setPosition(x, y);
			spr.setFlippedHorizontal(Array_spr[index].isFlippedHorizontal());
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		

		float x, y;
		x = Utils.GetAnchorX(spr.getWidth(), Array_spr[index].getAnchor()) + Utils.GetRatio(Array_spr[index].getX());
		y = Utils.GetAnchorY(spr.getHeight(), Array_spr[index].getAnchor()) + Utils.GetRatio(Array_spr[index].getY());
		
		spr.setPosition(x, y);
		spr.setFlippedHorizontal(Array_spr[index].isFlippedHorizontal());

	}
	
	public void AddText(AText text)
	{
		spr.attachChild(text.theText());
		int x, y;
		x = (int) (spr.getWidth()/2 - text.theText().getWidth()/2);
		y = (int) (spr.getHeight()/2 - text.theText().getHeight()/2);
		text.theText().setPosition(x, y);
	}
	
	public void AddLeftText(AText text)
	{
		spr.attachChild(text.theText());
		int x, y;
		x = (int) (spr.getWidth());
		y = (int) (spr.getHeight()/2 - text.theText().getHeight()/2);
		text.theText().setPosition(x, y);
	}
	
	public void AddSprite(ASprite sprite)
	{
		spr.attachChild(sprite.theSprite());
		int x, y;
		x = (int) (spr.getWidth()/2 - sprite.theSprite().getWidth()/2);
		y = (int) (spr.getHeight()/2 - sprite.theSprite().getHeight()/2);
		sprite.theSprite().setPosition(x, y);
	}
	
	public Sprite theSprite()
	{
		return spr;
	}
	
	public void SetPos(float x, float y, Anchor anchor)
	{
		float _x, _y;
		_x = Utils.GetAnchorX(spr.getWidth(), anchor) + Utils.GetRatio(x);
		_y = Utils.GetAnchorY(spr.getHeight(), anchor) + Utils.GetRatio(y);
		
		SetPos(_x, _y);
	}
	
	public void SetPos(float x, float y)
	{
		spr.setPosition(x, y);
	}
	
	public void setScale(float scaleRatio)
	{
		spr.setScale(scaleRatio);
	}
	
	public void DetachSelf()
	{
		spr.detachSelf();
	}
	
	public float GetX()
	{
		return spr.getX();
	}
	
	public float GetY()
	{
		return spr.getY();
	}
	
	public void unLoad()
	{
//		bbta.unload();
	}
	
}
